﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Backend.JYD.Model.Bid;
using Backend.JYD.Model;

namespace Backend.JYD.EFDAL.DAL
{
    public class InviteInfoDAL : IDAL.Bid.IInviteInfo
    {
        #region IInviteInfo 成员

        public long AddInviteInfo(Model.Bid.InviteInfo info)
        {
            using (JydDataContext db = new JydDataContext())
            {
                db.InviteInfoes.Add(info);
                db.SaveChanges();
                return info.InviteId;
            }
        }

        public bool UpdateInviteInfo(Model.Bid.InviteInfo info)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.InviteInfoes.Where(p => p.InviteId == info.InviteId);
                var temp = query.FirstOrDefault();
                if (temp != null)
                {
                    temp = info;
                    return db.SaveChanges() > 0;
                }
                return false;
            }
        }

        public Model.Bid.InviteInfo GetInviteInfoByInviteId(long inviteId)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.InviteInfoes.Where(p => p.InviteId == inviteId);
                return query.FirstOrDefault();
            }
        }

        public IList<InviteInfo> GetInviteInfoByCustNo(string custNo)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.InviteInfoes.Where(p => p.CustNo  == custNo);
                return query == null ? null : query.ToList();
            }
        }

        public IList<InviteInfo> GetAutoInviteInfo(int status, DateTime startDate)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.InviteInfoes.Where(p => p.Status == status && p.BidStartDate.Value.CompareTo(startDate) <= 0);
                return query == null ? null : query.ToList();
            }
        }

        public IList<InviteInfo> GetFlowInviteInfo(int status, DateTime endDate)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.InviteInfoes.Where(p => p.Status == status && p.BidEndDate.Value.CompareTo(endDate) <= 0);
                return query == null ? null : query.ToList();
            }
        }

        public PageInfo<InviteInfo> GetRunInviteInfoByPage(PageInfo page, int[] status)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.InviteInfoes.Where(p => p.Status.HasValue && status.Contains(p.Status.Value));
                return new PageInfo<InviteInfo>(page, query);
            }
        }

        #endregion
    }
}
